package com.app.view.component.dialogs 
{
	import flash.text.TextField;
	import flash.events.MouseEvent;
	
	import com.peachcastle.core.Dialog;
	import com.app.view.manager.DialogManagerMediator;
	import com.app.view.component.RoomMediator;
	import org.component.Button;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class ExchangeView extends Dialog
	{
		private static const CHANGE:int = 100;
		
		private var _dialogManagerMediator:DialogManagerMediator = null;
		
		private var _minChip:int = 100;
		private var _maxChip:int = 2000;
		
		private var _mcBtnMinChip:Button = null;
		private var _mcBtnMaxChip:Button = null;
		
		private var _btnDecrement:Button = null;
		private var _btnAddition:Button = null;
		
		private var _btnClose:Button = null;
		private var _btnExchange:Button = null;
		
		private var _userChip:TextField = null;
		private var _txtCurrentChip:TextField = null;
		
		public function ExchangeView(dialogUrlFunc:Function, dialogManagerMediator:DialogManagerMediator) 
		{
			_dialogManagerMediator = dialogManagerMediator;
			super("ExchangeView", true, dialogUrlFunc, 
					_dialogManagerMediator.stageSize["width"], _dialogManagerMediator.stageSize["height"]);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			super.move(centerX + 140, 170);
			
			_mcBtnMinChip = new Button(assest["mc_btn_min_chip"]);
			_mcBtnMaxChip = new Button(assest["mc_btn_max_chip"]);
			
			_btnDecrement = new Button(assest["mc_btn_decrement"]);
			_btnAddition = new Button(assest["mc_btn_addition"]);
			
			_btnClose = new Button(assest["btn_close"]);
			_btnExchange = new Button(assest["mc_btn_exchange"]);
			
			_txtCurrentChip = assest["txt_current_chip"];
			
			_userChip = assest["txt_user_chip"];
		}
		
		override protected function configureEventListeners():void 
		{
			super.configureEventListeners();
			
			_btnClose.addEventListener(MouseEvent.CLICK, onClose);
			
			_btnDecrement.addEventListener(MouseEvent.CLICK, onChange);
			_btnAddition.addEventListener(MouseEvent.CLICK, onChange);
			
			_mcBtnMinChip.addEventListener(MouseEvent.CLICK, onExchange);
			_mcBtnMaxChip.addEventListener(MouseEvent.CLICK, onExchange);
			_btnExchange.addEventListener(MouseEvent.CLICK, onExchange);
		}
		
		override protected function showAction():void 
		{
			super.showAction();	
			
			_userChip.text = data["userChip"];
			_maxChip = data["minPour"] * 150;
			if (_maxChip <= 0) _maxChip = 2000;
			
			_mcBtnMinChip.setLabel(String(_minChip));
			_mcBtnMaxChip.setLabel(String(_maxChip));
			
			_txtCurrentChip.text = String(_minChip);
		}
		
		private function onClose(e:MouseEvent):void
		{
			super.hide();
		}
		
		private function onChange(e:MouseEvent):void
		{
			switch(e.target.assest.name)
			{
				case "mc_btn_decrement":
					if (int(_txtCurrentChip.text) - CHANGE < _minChip)
					{
						_txtCurrentChip.text = String(_minChip);
					}else
					{
						_txtCurrentChip.text = String(int(_txtCurrentChip.text) - CHANGE);
					}
					
					break;
					
				case "mc_btn_addition":
					if (int(_txtCurrentChip.text) + CHANGE > _maxChip)
					{
						_txtCurrentChip.text = String(_maxChip);
					}else
					{
						_txtCurrentChip.text = String(int(_txtCurrentChip.text) + CHANGE);
					}
					
					break;
			}
		}
		
		private function onExchange(e:MouseEvent):void
		{
			var chip:int = 0;
			switch(e.target.assest.name)
			{
				case "mc_btn_min_chip":
					chip = _minChip;
					
					break;
					
				case "mc_btn_max_chip":
					chip = _maxChip;
					
					break;
					
				case "mc_btn_exchange":
					chip = int(_txtCurrentChip.text);
					
					break;
			}
			
			_dialogManagerMediator.sendNotification(RoomMediator.REAL_SIT_DOWN_REQUEST, [chip]);
			
			super.hide();
		}
	}

}